var app = getApp();
var ajax = require('../../utils/ajax.js');
var util = require('../../utils/util.js');
var common = require('../../utils/common.js');
var MPage = require('../MPage.js');
var begin_time = "";
var end_time = "";

Page(common.extendPage(MPage, {
    data: {
        dataLoaded: false,
        loading: false,
        moreLoading: false,
        couponList: [],
        page: 1,
        noMore: false,
        already: false,

        currentTab: 0,

        isLogin: false,
        isLoginShow: false,
    },

    onLoad: function(options) {
        var self = this;

        self._initData({}, function() {
            self.setData({
                dataLoaded: true,
                isLogin: !!app.getUserSid(),
            });
        });
    },

    onLoginSuc() {
        this.setData({
            isLogin: true
        });
    },

    _showLogin() {
        this.setData({
            isLoginShow: true
        });
    },

    /**
     * 事件切换tab
     */
    _tabSwitch: function(evt) {
        var index = +evt.currentTarget.dataset.current;
        var formId = evt.detail.formId;

        this._toTab(index);
        common.submitForm(formId);
    },

    _toTab: function(index, cb) {
        this.setData({
            currentTab: index,
        });
    },

    _initData: function(params, cb) {
        this.setData({
            couponList: [],
            page: 1,
            noMore: false,
        });
        this._getReceiveCouponList({}, cb);
    },

    _getReceiveCouponList: function(params, cb) {
        var self = this;
        var isMore = params.isMore;
        var page = self.data.page || 1;
        var url = '/openapi/CCoupon/getCouponList';
        if (isMore) {
            self.setData({
                moreLoading: true
            });
        } else {
            self.setData({
                loading: true
            });
        }
        ajax.P(url, {
            page: page
        }, function(data) {
            var couponList = data.result.couponList || [];

            couponList.forEach(function(coupon) {
                var money = parseInt(coupon.coupon_amount);
                if (money == coupon.coupon_amount) {
                    coupon.coupon_amount = money;
                }
            });

            var toData = {
                couponList: self.data.couponList.concat(couponList),
                noMore: couponList.length == 0,
                page: self.data.page + 1,
            };

            toData[isMore ? 'moreLoading' : 'loading'] = false;
            self.setData(toData);
            cb && cb();
        });
    },
    /**
     * 加载更多
     */
    _loadMore: function() {
        if (this.data.moreLoading || this.data.loading || this.data.noMore) {
            return;
        }
        this._getReceiveCouponList({
            isMore: true
        });
    },

    //领取优惠券
    _receive: function(e) {
        var _self = this;
        var formId = e.detail.formId;
        var couponList = this.data.couponList;
        var coupon_cfg_id = e.currentTarget.dataset.coupon_id;

        if (!this.data.isLogin) {
            this._showLogin();
            return;
        }
        ajax.P('/openapi/CCoupon/activeCoupon', {
                coupon_cfg_id
            }, function(data) {
                util.alert('优惠券领取成功', function() {
                    couponList.forEach(function(item) {
                        if (coupon_cfg_id == item.id) {
                            item.is_active = 1;
                        }
                    })
                    _self.setData({
                        couponList: couponList
                    })
                });
            }),
            common.submitForm(formId);
    },

    /**
     * 兑换优惠券
     * @param  {[type]} evt [description]
     * @return {[type]}     [description]
     */
    _exchange(evt) {
        if (!this.data.isLogin) {
            this._showLogin();
            return;
        }
        wx.showLoading({
            title: '兑换中'
        });
        ajax.P('/openapi/CCoupon/exchangeCoupon', evt.detail.value, (res) => {
            wx.hideLoading();
            util.alert('优惠券兑换成功', function() {
                wx.navigateTo({
                    url: '/pages/coupon/coupon'
                });
            })
        }, () => {
            wx.hideLoading();
        });
    },

}));